package com.tpyule.tpclass.service.impl;

import com.tpyule.tpclass.dao.AnswerDao;
import com.tpyule.tpclass.entity.bean.SubscriptionAction;
import com.tpyule.tpclass.entity.bo.AnswerDetailBo;
import com.tpyule.tpclass.entity.dto.AnswerQueryDto;
import com.tpyule.tpclass.entity.pojo.Answer;
import com.tpyule.tpclass.entity.pojo.Notify;
import com.tpyule.tpclass.entity.pojo.Subscription;
import com.tpyule.tpclass.service.AnswerService;
import com.tpyule.tpclass.service.NotifyService;
import com.tpyule.tpclass.service.SubscriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/26 21:41
 */
@Service
public class AnswerServiceImpl implements AnswerService {
    @Autowired
    AnswerDao answerDao;
    @Autowired
    SubscriptionService subscriptionService;
    @Autowired
    NotifyService notifyService;
    @Override
    public int addAnswer(Answer answer) {
        int result = 0;
        if (answer!=null){
            answer.setAnswerId(UUID.randomUUID().toString().toUpperCase());
            result = answerDao.addAnswer(answer);

            if(answer.getLevel()==2){
                //提醒 新回答
                Notify notify = new Notify();
                notify.setType(2);
                notify.setAction(SubscriptionAction.new_answer);
                notify.setRemove(1);
                notify.setSender(answer.getUserId());
                notify.setTargetId(answer.getReplyAnswerId());
                notify.setTargetType("answer");
                notifyService.createRemind(notify);
            }
            else if(answer.getLevel()==1) {

                //提醒 新回答
                Notify notify = new Notify();
                notify.setType(2);
                notify.setAction(SubscriptionAction.new_answer);
                notify.setRemove(1);
                notify.setSender(answer.getUserId());
                notify.setTargetId(answer.getQuestionId());
                notify.setTargetType("question");
                notifyService.createRemind(notify);

                //提醒 关注的问题新回答
                Notify notify1 = new Notify();
                notify1.setType(2);
                notify1.setAction(SubscriptionAction.f_new_answer);
                notify1.setRemove(1);
                notify1.setSender(answer.getUserId());
                notify1.setTargetId(answer.getQuestionId());
                notify1.setTargetType("question");
                notifyService.createRemind(notify1);

                //订阅 回答采纳提醒
                Subscription subscription = new Subscription();
                subscription.setSubscriptionId(UUID.randomUUID().toString().toUpperCase());
                subscription.setTargetId(answer.getAnswerId());
                subscription.setTargetType("answer");
                subscription.setAction(SubscriptionAction.yes_answer);
                subscription.setUserId(answer.getUserId());
                subscription.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                subscriptionService.addSubscription(subscription);
            }
            //订阅 回答被回复
            Subscription subscription1 = new Subscription();
            subscription1.setSubscriptionId(UUID.randomUUID().toString().toUpperCase());
            subscription1.setTargetId(answer.getAnswerId());
            subscription1.setTargetType("answer");
            subscription1.setAction(SubscriptionAction.ana);
            subscription1.setUserId(answer.getUserId());
            subscription1.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            subscriptionService.addSubscription(subscription1);
        }
        return result;
    }

    @Override
    public List<AnswerDetailBo> getListAnswer(AnswerQueryDto dto) {
        dto.setLevel(1);
        List<AnswerDetailBo> bos = answerDao.getListAnswer(dto);
        for (AnswerDetailBo bo : bos) {
            AnswerQueryDto cdto = new AnswerQueryDto();
            cdto.setQuestionId(bo.getQuestionId());
            cdto.setLevel(2);
            cdto.setParentAnswerId(bo.getAnswerId());
            List<AnswerDetailBo> cbos = answerDao.getListAnswer(cdto);
            bo.setChildren(cbos);
        }
        return bos;
    }

    @Override
    public int updateAnswer(Answer answer) {
        return answerDao.updateAnswer(answer);
    }
}
